﻿using EF测试DbSet.Models;
using System.Data;
using System.Linq;

namespace EF测试DbSet
{
    class Program
    {
        static void Main(string[] args)
        {
            #region EF 代码
            //BaseContext db = new BaseContext();

            //PersonPhone p1 = new PersonPhone()
            //{
            //    Id = 0,
            //    Name = "张三",
            //    Phone = "18931608223",
            //    PhoneType = 1,
            //    IsDefault = 1,
            //    CreateTime = DateTime.Now
            //};

            //db.Set<PersonPhone>().Add(p1);


            //int a = db.ChangeTracker.Entries<PersonPhone>().Count(x => x.Entity.IsDefault == 0);

            //PersonPhone p2 = new PersonPhone()
            //{
            //    Id = 0,
            //    Name = "张三",
            //    Phone = "03105159999",
            //    PhoneType = 1,
            //    IsDefault = 0,
            //    CreateTime = DateTime.Now
            //};


            //db.Set<PersonPhone>().Add(p2);


            //int b = db.ChangeTracker.Entries<PersonPhone>().Count(x => x.Entity.IsDefault == 0 && x.State == EntityState.Added);

            //PersonPhone p3 = new PersonPhone()
            //{
            //    Id = 0,
            //    Name = "张三",
            //    Phone = "0310-564889",
            //    PhoneType = 1,
            //    IsDefault = 1,
            //    CreateTime = DateTime.Now
            //};

            //if (db.Set<PersonPhone>().Any(x => x.IsDefault > 0))
            //{
            //    db.Set<PersonPhone>().Add(p3);
            //}

            //db.SaveChanges(); 

            //using (var db=new BaseContext())
            //{
            //    var data = db.Set<BaseUser>().Where(x => x.CreateTime > SqlFunctions.DateAdd("dd", -30, x.CreateTime));



            //    var data2= db.Set<BaseUser>().Where(x => x.CreateTime > x.CreateTime.AddDays(-30));
            //    //.ToList();
            //}
            #endregion

            //BaseContext db = new BaseContext();

            //DataTable dt = JsonConvert.DeserializeObject<DataTable>(JsonConvert.SerializeObject(db.BaseUsers));

            //using (var db = new BaseContext())
            //{
            //    var data = db.Set<BaseUser>().Where(x => x.Id.Equals(1)).ToList();
            //}

            //using (var db = new BaseContext())
            //{
            //    var data = db.PersonPhones.Where(x => db.BaseUsers.Select(b => b.Id).Contains(x.Id)).ToList();
            //}

            //using (var db = new BaseContext())
            //{
            //    var data = db.PersonPhones.ToList();
            //    string strStatus = "200";
            //    string strResult = JsonConvert.SerializeObject(data);
            //    //string result = "{\"status\":\"" + strStatus + "\",\"result\":\"" + strResult + "\"}";
            //    //Result res=new Result();
            //    //res.Data = strResult;
            //    //res.strStatus = 200;
            //    string ss=strResult.Replace("\"","\\\"");
            //    //string json = JsonConvert.SerializeObject(res);
            //   string result1 = "{\"status\":\"" + strStatus + "\",\"result\":\"" + ss + "\"}";

            //   Result res2 = JsonConvert.DeserializeObject<Result>(result1);
            //   List<PersonPhone> ps = JsonConvert.DeserializeObject<List<PersonPhone>>(res2.result);

            //    Console.WriteLine("");
            //}

            #region 手动拼接Json
            //DataTable dt = SqlUtil.GetTable(SqlUtil.connString, "Pro_GetArticleReview", CommandType.StoredProcedure,
            //        new SqlParameter[]
            //            {new SqlParameter("@StartDate", DateTime.Now), new SqlParameter("@EndDate", DateTime.Now)});

            //string res = JsonConvert.SerializeObject(dt);

            //StringBuilder sb = new StringBuilder();
            //sb.Append("[");
            //foreach (DataRow row in dt.Rows)
            //{
            //    sb.Append("{");
            //    foreach (DataColumn column in dt.Columns)
            //    {
            //        sb.Append("\"" + column.ColumnName + "\"" + ":" + "\"" + row[column.ColumnName] + "\",");
            //    }
            //    sb.Remove(sb.Length - 1, 1);
            //    sb.Append("},");
            //}
            //sb.Remove(sb.Length - 1, 1);
            //sb.Append("]");

            //string res2 = sb.ToString();
            //Console.ReadKey(); 
            #endregion

            string name = null;
            using (var db = new BaseContext())
            {
                var data = db.BaseUsers.Where(x => x.Name.Contains(name)).ToList();
            }

            //DataTable dt = JsonConvert.DeserializeObject<DataTable>(JsonConvert.SerializeObject(ListData));

        }
    }
}
